home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter3 / isohex3_5 / isohex3_5.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-18  |  6.4 KB  |  269 lines

  1. /*****************************************************************************
  2. IsoHex3_5.cpp
  3. Ernest S. Pazera
  4. 18MAY2000
  5. Start a WIN32 Application Workspace, add in this file
  6. No other libs are required
  7. *****************************************************************************/
  8.  
  9. //////////////////////////////////////////////////////////////////////////////
  10. //INCLUDES
  11. //////////////////////////////////////////////////////////////////////////////
  12. #define WIN32_LEAN_AND_MEAN  
  13.  
  14. #include <windows.h>   
  15.  
  16. //////////////////////////////////////////////////////////////////////////////
  17. //DEFINES
  18. //////////////////////////////////////////////////////////////////////////////
  19. //name for our window class
  20. #define WINDOWCLASS "ISOHEX3"
  21. //title of the application
  22. #define WINDOWTITLE "IsoHex 3-5"
  23.  
  24. //bitmap sizes
  25. const int BITMAPHEIGHT=32;
  26. const int BITMAPWIDTH=64;
  27.  
  28. //////////////////////////////////////////////////////////////////////////////
  29. //PROTOTYPES
  30. //////////////////////////////////////////////////////////////////////////////
  31. bool Prog_Init();//game data initalizer
  32. void Prog_Loop();//main game loop
  33. void Prog_Done();//game clean up
  34.  
  35. //show the cursor
  36. void ShowTheCursor(HDC hdc);
  37.  
  38. //////////////////////////////////////////////////////////////////////////////
  39. //GLOBALS
  40. //////////////////////////////////////////////////////////////////////////////
  41. HINSTANCE hInstMain=NULL;//main application handle
  42. HWND hWndMain=NULL;//handle to our main window
  43. //memory dc
  44. HDC hdcMem=NULL;
  45. //bitmaps
  46. HBITMAP hbmNew=NULL;
  47. HBITMAP hbmOld=NULL;
  48. //cursor location
  49. POINT ptCursor;
  50.  
  51. //////////////////////////////////////////////////////////////////////////////
  52. //WINDOWPROC
  53. //////////////////////////////////////////////////////////////////////////////
  54. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  55. {
  56.     //which message did we get?
  57.     switch(uMsg)
  58.     {
  59.     case WM_MOUSEMOVE:
  60.         {
  61.             //extract x and y from lParam
  62.             int x=LOWORD(lParam);
  63.             int y=HIWORD(lParam);
  64.  
  65.             //borrow window's dc
  66.             HDC hdc=GetDC(hWndMain);
  67.  
  68.             //write the cursor
  69.             ShowTheCursor(hdc);
  70.  
  71.             //update the cursor position
  72.             ptCursor.x=x;
  73.             ptCursor.y=y;
  74.  
  75.             //write the cursor
  76.             ShowTheCursor(hdc);
  77.  
  78.             //return dc to system
  79.             ReleaseDC(hWndMain,hdc);
  80.  
  81.             //handled, so return 0
  82.             return(0);
  83.         }break;
  84.     case WM_DESTROY://the window is being destroyed
  85.         {
  86.  
  87.             //tell the application we are quitting
  88.             PostQuitMessage(0);
  89.  
  90.             //handled message, so return 0
  91.             return(0);
  92.  
  93.         }break;
  94.     case WM_PAINT://the window needs repainting
  95.         {
  96.             //a variable needed for painting information
  97.             PAINTSTRUCT ps;
  98.             
  99.             //start painting
  100.             HDC hdc=BeginPaint(hwnd,&ps);
  101.  
  102.             /////////////////////////////
  103.             //painting code would go here
  104.             /////////////////////////////
  105.             ShowTheCursor(hdc);
  106.  
  107.             //end painting
  108.             EndPaint(hwnd,&ps);
  109.                         
  110.             //handled message, so return 0
  111.             return(0);
  112.         }break;
  113.     }
  114.  
  115.     //pass along any other message to default message handler
  116.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  117. }
  118.  
  119.  
  120. //////////////////////////////////////////////////////////////////////////////
  121. //WINMAIN
  122. //////////////////////////////////////////////////////////////////////////////
  123. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  124. {
  125.     //assign instance to global variable
  126.     hInstMain=hInstance;
  127.  
  128.     //create window class
  129.     WNDCLASSEX wcx;
  130.  
  131.     //set the size of the structure
  132.     wcx.cbSize=sizeof(WNDCLASSEX);
  133.  
  134.     //class style
  135.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  136.  
  137.     //window procedure
  138.     wcx.lpfnWndProc=TheWindowProc;
  139.  
  140.     //class extra
  141.     wcx.cbClsExtra=0;
  142.  
  143.     //window extra
  144.     wcx.cbWndExtra=0;
  145.  
  146.     //application handle
  147.     wcx.hInstance=hInstMain;
  148.  
  149.     //icon
  150.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  151.  
  152.     //cursor
  153.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  154.  
  155.     //background color
  156.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  157.  
  158.     //menu
  159.     wcx.lpszMenuName=NULL;
  160.  
  161.     //class name
  162.     wcx.lpszClassName=WINDOWCLASS;
  163.  
  164.     //small icon
  165.     wcx.hIconSm=NULL;
  166.  
  167.     //register the window class, return 0 if not successful
  168.     if(!RegisterClassEx(&wcx)) return(0);
  169.  
  170.     //create main window
  171.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  172.  
  173.     //error check
  174.     if(!hWndMain) return(0);
  175.  
  176.     //if program initialization failed, then return with 0
  177.     if(!Prog_Init()) return(0);
  178.  
  179.     //message structure
  180.     MSG msg;
  181.  
  182.     //message pump
  183.     for(;;)    
  184.     {
  185.         //look for a message
  186.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  187.         {
  188.             //there is a message
  189.  
  190.             //check that we arent quitting
  191.             if(msg.message==WM_QUIT) break;
  192.             
  193.             //translate message
  194.             TranslateMessage(&msg);
  195.  
  196.             //dispatch message
  197.             DispatchMessage(&msg);
  198.         }
  199.  
  200.         //run main game loop
  201.         Prog_Loop();
  202.     }
  203.     
  204.     //clean up program data
  205.     Prog_Done();
  206.  
  207.     //return the wparam from the WM_QUIT message
  208.     return(msg.wParam);
  209. }
  210.  
  211. //////////////////////////////////////////////////////////////////////////////
  212. //INITIALIZATION
  213. //////////////////////////////////////////////////////////////////////////////
  214. bool Prog_Init()
  215. {
  216.     //borrow dc from main window
  217.     HDC hdc=GetDC(hWndMain);
  218.  
  219.     //create a memory dc
  220.     hdcMem=CreateCompatibleDC(hdc);
  221.  
  222.     //load in the bitmap
  223.     hbmNew=(HBITMAP)LoadImage(NULL,"IsoHex3_5.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
  224.  
  225.     //select bitmap into memory dc
  226.     hbmOld=(HBITMAP)SelectObject(hdcMem,hbmNew);
  227.  
  228.     //set original cursor position
  229.     ptCursor.x=0;
  230.     ptCursor.y=0;
  231.  
  232.     //return dc to system
  233.     ReleaseDC(hWndMain,hdc);
  234.  
  235.     return(true);//return success
  236. }
  237.  
  238. //////////////////////////////////////////////////////////////////////////////
  239. //CLEANUP
  240. //////////////////////////////////////////////////////////////////////////////
  241. void Prog_Done()
  242. {
  243.     //restore old bitmap to memory dc
  244.     SelectObject(hdcMem,hbmOld);
  245.  
  246.     //delete bitmap
  247.     DeleteObject(hbmNew);
  248.  
  249.     //delete dc
  250.     DeleteDC(hdcMem);
  251. }
  252.  
  253. //////////////////////////////////////////////////////////////////////////////
  254. //MAIN GAME LOOP
  255. //////////////////////////////////////////////////////////////////////////////
  256. void Prog_Loop()
  257. {
  258.     ///////////////////////////
  259.     //main game logic goes here
  260.     ///////////////////////////
  261. }
  262.  
  263. //show the cursor
  264. void ShowTheCursor(HDC hdc)
  265. {
  266.     BitBlt(hdc,ptCursor.x-BITMAPWIDTH/2,ptCursor.y-BITMAPHEIGHT/2,BITMAPWIDTH,BITMAPHEIGHT,hdcMem,0,0,SRCINVERT);
  267. }
  268.  
  269.